package com.faiz.picturebed.controller;

import com.faiz.picturebed.common.R;
import com.faiz.picturebed.service.ParseMDService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * <p>
 * 图床 前端控制器
 * </p>
 *
 * @author faiz
 * @since 2021-07-14
 */
@Api(value = "markdown本地图片图片替换阿里云oss工具")
@RestController
@RequestMapping("/parse")
public class ParseMDController {

    private final ParseMDService parseMDService;

    @Autowired
    public ParseMDController(ParseMDService parseMDService) {
        this.parseMDService = parseMDService;
    }

    @ApiOperation(value = "上传图片文件")
    @PostMapping(value = "/parseMDFile", headers = "content-type=multipart/form-data")
    public R parse(@ApiParam(value = "file", required = true) @RequestPart("file") MultipartFile file, HttpServletResponse response) {
        try {
            parseMDService.parseMD(file, response);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return R.ok().data("msg","md文件解析成功");
    }
}
